<?php

namespace app\personal\controller;

use library\Controller;
use app\common\service\CompressService;
use app\common\service\WxTokenService;
use library\File;
use think\Db;

/**
 * 应用入口
 * Class Index
 * @package app\personal\controller
 */
class Index extends Base
{
    /**
     * 首页
     */
    public function index()
    {
    	if(isset($_GET['code'])){
			$code = $this->request->get('code');
			$wxtoken = new WxTokenService();
        	$openid = $wxtoken->getOpenidFromMp($code);
        	session('wechat_openid', $openid);
		}else{
			$wechat_openid = session('wechat_openid');
			if(empty($wechat_openid) || !$wechat_openid){
				session('wechat_openid', '');
			}			
		}
	  	$member = $this->member; 
	  	$increase =  Db::name('system_increase')->where(['id_number'=>$member['loginname'],'is_deleted'=>0])->order('id desc')->field('id,name,id_number,mobile,person_id,company_id,contract_id,status,examine,id_front,id_back')->find();
	  	if($increase){
	  		//查看本公司档案
	  		$archives = Db::name('system_archives')->where(['id_number'=>$member['loginname'],'company_id'=>$increase['company_id']])->field('status,into_date,out_date,person_status')->find();
	  		$this->archives = $archives; 
	  		
	  		if($increase['status'] == 2){
	  			//查看在职状态
	  			$person = Db::name('system_person')->where(['id'=>$increase['person_id']])->field('id,status')->find();
	  			if($person['status'] != 1){
	  				//已离职
	  				$page_type = 1;
	  				//查找离职后的险种状态
	  				$insurances = Db::name('system_insurance_result')->where(['person_id'=>$increase['person_id'],'company_id'=>$increase['company_id']])->order('insurance asc')->field('id,insurance,status,start_date,end_date')->select();
	  				if($insurances){
	  					foreach($insurances as $k=>$v){
	  						$insurances[$k]['insurance'] = get_option('insurance')[$v['insurance']];
							$insurances[$k]['status_cn'] = ['1'=>'在保 ','2'=>'解约','3'=>'停保'][$v['status']];
	  					}
	  					$this->insurances = $insurances;
	  				}
	  			}else{
	  				//在职，显示险种的参保状态
	  				$insurances = Db::name('system_insurance_result')->where(['person_id'=>$increase['person_id'],'company_id'=>$increase['company_id']])->order('insurance asc')->field('id,insurance,status,start_date,end_date')->select();
	  				if(!$insurances){
	  					//已审核，未确认
	  					$page_type = 2;
	  				}else{
	  					//已确认有险种信息
	  					$page_type = 3;
	  					foreach($insurances as $k=>$v){
	  						$insurances[$k]['insurance'] = get_option('insurance')[$v['insurance']];
							$insurances[$k]['status_cn'] = ['1'=>'在保 ','2'=>'解约','3'=>'停保'][$v['status']];
	  					}
	  					$this->insurances = $insurances;
	  				}	  				
	  			}
	  		}else{
	  			//增员审核中
	  			$page_type = 4;
	  			$increase_insurances = Db::name('system_increase_insurance')->where(['increase_id'=>$increase['id'],'status'=>1])->order('insurance asc')->field('id,insurance,hand_type')->select();
	  			if($increase_insurances){
	  				$is_show = 0;
	  				foreach($increase_insurances as $k=>$v){
  						$increase_insurances[$k]['insurance'] = get_option('insurance')[$v['insurance']];
  						if($v['hand_type']){
  							$is_show = 1;
  							$hand_type = get_option('hand_type',$v['insurance']);
							$increase_insurances[$k]['hand_type'] = $hand_type[$v['hand_type']];
  						}else{
  							$increase_insurances[$k]['hand_type'] = '';
  						}  						
  					}
	  			}
	  			$this->increase_insurances = $increase_insurances;
	  			$this->is_show = $is_show;
	  		}
	  		$company = Db::name('system_company')->where(['id'=>$increase['company_id']])->field('id,name')->find();
	  		$this->company = $company;
	  		$this->page_type = $page_type;	  		
	  	}
	  	
	  	$this->increase = $increase; 	
		$this->fetch();
		   
    }
    
    
    
    /**
     * 更改手机绑定
     */
    public function changemb()
    {
    	if ($this->request->isPost()) {
    		$member = $this->member; 
    		//手机号验证码登录  
    		$data = $this->_input([
                'mobile' => trim($this->request->post('mobile')),
                'verify' => trim($this->request->post('verify')),
            ], [
                'mobile' => 'require|mobile',
                'verify' => 'require|number|length:4',
            ], [
                'mobile.require' => '手机号必须填写',
                'mobile.mobile' => '手机号格式错误',
                'verify.require' => '验证码必须填写',
                'verify.number' => '验证码格式错误',
                'verify.length' => '验证码为4位',
            ]);
            $verify = Db::name('system_verify')
						->where(['mobile'=>$data['mobile'],'status'=>0,'type'=>4])
						->order('create_at desc')
						->find();
			if(!$verify || $data['verify'] != $verify['code']){
				$this->error('验证码错误');
			}		
			if(time() - $verify['create_at'] > 900){
				$this->error('验证码已失效');
			}
			$find = Db::name('SystemMember')->where([['id','neq',$member['id']],['mobile','eq',$data['mobile']],['type','eq',2]])->find();
			if($find){
				$this->error('手机号已存在，无法修改');
			}
	        $re = Db::name('SystemMember')->where(['id'=>$member['id']])->update(['mobile'=>$data['mobile']]);
	       	if($re){
	       		$this->success('修改成功');
	       	}else{
	       		$this->error('请更换修改的手机号');
	       	}
    	} 
    }
    
    
    
    /**
     * 身份证上传
     */
    public function idupload()
    {    	
    	if ($this->request->isPost()) {
    		$data = $this->request->post();
    		if(empty($data) || !$data){
				$this->error('数据错误');
			}
			if(empty($data['idfront']) || !$data['idfront']){
				$this->error('身份证必传');
			}
			if(empty($data['idback']) || !$data['idback']){
				$this->error('身份证必传');
			}
			$member = $this->member; 
			$increase =  Db::name('system_increase')->where(['id'=>$data['id'],'is_deleted'=>0])->field('id,name,id_number,status,examine,id_front,id_back')->find();
			if(!$increase){
				$this->error('参数错误');
			}
			if($increase['id_number'] != $member['loginname']){
				$this->error('参数错误');
			} 	
			if(($increase['status'] != 1) || ($increase['examine'] != 2)){
				$this->error('参数错误');
			}  
			Db::name('system_increase')->where(['id'=>$data['id']])->update(['id_front'=>$data['idfront'],'id_back'=>$data['idback']]);
			$this->success('上传成功');
    	}else{ 
    		$id = $this->request->get('id');
			$member = $this->member; 
			$increase =  Db::name('system_increase')->where(['id'=>$id,'is_deleted'=>0])->field('id,name,id_number,status,examine,id_front,id_back')->find();
			if(!$increase){
				$this->error('参数错误');
			}
			if($increase['id_number'] != $member['loginname']){
				$this->error('参数错误');
			} 	
			if(($increase['status'] != 1) || ($increase['examine'] != 2)){
				$this->error('参数错误');
			}      		
			$this->increase = $increase;
			$this->id = $id;
    		$this->fetch();
    	}   					   
    }
    
    
    /**
     * 身份证上传--自定义裁剪
     */
    public function upload(){
	    // 获取表单上传文件 例如上传了001.jpg
	    $file = request()->file('image');
		if (empty($file) || !$file) {
            $this->error('文件上传异常，文件可能过大或未上传');
       	} 
        if (!$file->checkExt('jpg,png,jpeg')) {
//          return json(['uploaded' => false, 'error' => ['message' => '文件上传类型错误']]);
			$this->error('文件上传类型错误');
        }
        if ($file->checkExt('php,sh')) {
            $this->error('可执行文件禁止上传');
        }
        if (!$file->checkSize(10485760)) {
            $this->error('图片大小不能超过10M');
        }	
		//图片压缩
		$path = 'upload/member/idcard';
        if(!file_exists($path)){
            mkdir($path,0777,true);
        }
        $image_name = md5(time().$file->getPathname());
        $dst_img = $path.'/'.$image_name.'.'.pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
        $percent = 1;  #原图压缩，不缩放，但体积大大降低
        $image = (new CompressService($file->getPathname(),$percent))->compressImg($dst_img);
		
	    //阿里云OSS上传
		$date = date('Ymd',time());
    	$dir = 'sheyoubao/member/idcard/'.$date.'/';
        $name = $dir.md5(md5_file($dst_img).uniqid(mt_rand())).'.'.pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
        
		$info = File::instance('oss')->save($name, file_get_contents($dst_img));
	    if(is_array($info) && isset($info['url'])){
	    	@unlink($dst_img); //删除压缩图片
	    	$idcardimg = 'https://files.wolundehr.com/'.$info['file'];
			$this->success('上传成功',$idcardimg);
	    }else{
	        // 上传失败获取错误信息
			$this->error($file->getError());
	    }
	}
    
    
    
    /**
     * 编辑保险状态
     */
    public function handtype()
    {		   	
		if ($this->request->isPost()) {
    		$data = $this->request->post();
    		if(empty($data) || !$data){
				$this->error('数据错误');
			}
			$member = $this->member; 
			$increase =  Db::name('system_increase')->where(['id'=>$data['id'],'is_deleted'=>0])->field('id,name,id_number,status,examine,id_front,id_back')->find();
			if(!$increase){
				$this->error('参数错误');
			}
			if($increase['id_number'] != $member['loginname']){
				$this->error('参数错误');
			} 	
			if(($increase['status'] != 1) || ($increase['examine'] != 2)){
				$this->error('参数错误');
			}
			if($data['types']){
				foreach($data['types'] as $k=>$v){
					$info = explode(',', $v);
					Db::name('system_increase_insurance')->where(['id'=>$info[0]])->update(['hand_type'=>$info[1]]);
				}
			}
			$this->success('修改成功');
    	}else{ 
    		$id = $this->request->get('id');
			$member = $this->member; 
			$increase =  Db::name('system_increase')->where(['id'=>$id,'is_deleted'=>0])->field('id,name,id_number,status,examine,id_front,id_back')->find();
			if(!$increase){
				$this->error('参数错误');
			}
			if($increase['id_number'] != $member['loginname']){
				$this->error('参数错误');
			} 	
			if(($increase['status'] != 1) || ($increase['examine'] != 2)){
				$this->error('参数错误');
			}
    		$increase_insurances = Db::name('system_increase_insurance')->where(['increase_id'=>$increase['id'],'status'=>1])->order('insurance asc')->field('id,insurance,hand_type')->select();
  			if($increase_insurances){
  				foreach($increase_insurances as $k=>$v){
					$increase_insurances[$k]['insurance_cn'] = get_option('insurance')[$v['insurance']];					  						
				}
  			}
  			$this->increase_insurances = $increase_insurances;      		
			$this->increase = $increase;
			$this->id = $id;
    		$this->fetch();
    	} 
		   
    }
    
    
    /**
     * 保险状态下拉项
     */
    public function handtype_option()
    {
    	//各险种状态
		$yl = $options = Db::name('system_option')->where(['type'=>'hand_type','is_deleted'=>0,'insurance'=>1])->order('sort desc,id asc')->field('title,code')->select(); 
    	foreach($yl as $k=>$v){
    		$yanglao[$k]['value'] = $v['code'];
    		$yanglao[$k]['title'] = $v['title'];
    	}
    	$gs = $options = Db::name('system_option')->where(['type'=>'hand_type','is_deleted'=>0,'insurance'=>2])->order('sort desc,id asc')->field('title,code')->select(); 
    	foreach($gs as $k=>$v){
    		$gongshang[$k]['value'] = $v['code'];
    		$gongshang[$k]['title'] = $v['title'];
    	}
		$sy = $options = Db::name('system_option')->where(['type'=>'hand_type','is_deleted'=>0,'insurance'=>3])->order('sort desc,id asc')->field('title,code')->select(); 
    	foreach($sy as $k=>$v){
    		$shiye[$k]['value'] = $v['code'];
    		$shiye[$k]['title'] = $v['title'];
    	}
		$yili = $options = Db::name('system_option')->where(['type'=>'hand_type','is_deleted'=>0,'insurance'=>4])->order('sort desc,id asc')->field('title,code')->select(); 
    	foreach($yili as $k=>$v){
    		$yiliao[$k]['value'] = $v['code'];
    		$yiliao[$k]['title'] = $v['title'];
    	}
    	$gjj = $options = Db::name('system_option')->where(['type'=>'hand_type','is_deleted'=>0,'insurance'=>5])->order('sort desc,id asc')->field('title,code')->select(); 
    	foreach($gjj as $k=>$v){
    		$gongjijin[$k]['value'] = $v['code'];
    		$gongjijin[$k]['title'] = $v['title'];
    	}
		$data = ['yanglao'=>$yanglao,'gongshang'=>$gongshang,'shiye'=>$shiye,'yiliao'=>$yiliao,'gongjijin'=>$gongjijin];
		$this->success('获取成功',$data);
    }

    
}
